package zzs.generalframework.console.core.parameter.commonparameter.jobtitle.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.parameter.commonparameter.jobtitle.JobTitleInfo;
import zzs.generalframework.common.core.parameter.commonparameter.jobtitle.JobTitlePO;
import zzs.generalframework.console.core.parameter.commonparameter.jobtitle.dao.IJobTitleDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.jobtitle.dao.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.parameter.commonparameter.jobtitle.dao.impl.jobTitleDaoImpl")
public class JobTitleDaoImpl implements IJobTitleDao
{
	private Logger logger = LoggerFactory.getLogger(JobTitleDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(JobTitlePO jobTitlePO)
	{
		logger.info("====插入职称数据开始====");
		logger.debug("====插入职称数据,入参:JobTitlePO=[{}]====", jobTitlePO);
		entityManager.persist(jobTitlePO);
		logger.debug("====插入职称数据,出参:boolean=[{}]====", true);
		logger.info("====插入职称数据结束====");
		return true;
	}

	@Override
	public boolean update(JobTitlePO jobTitlePO)
	{
		logger.info("====更新职称数据开始====");
		logger.debug("====更新职称数据,入参:JobTitlePO=[{}]====", jobTitlePO);
		entityManager.merge(jobTitlePO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新职称数据结束====");
		return true;
	}

	@Override
	public List<JobTitlePO> select(JobTitleInfo jobTitleInfo)
	{
		logger.info("====查询职称数据开始====");
		logger.debug("====查询职称数据,入参:JobTitleInfo=[{}]====", jobTitleInfo);
		List<JobTitlePO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from JobTitlePO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(jobTitleInfo.getJobTitleID()))
		{
			sqlBuffer.append(" and a.jobTitleID=?");
			parameter.putParameterObject(jobTitleInfo.getJobTitleID());
		}
		if (StringUtils.isNotNullAndBlank(jobTitleInfo.getJobTitleCode()))
		{
			sqlBuffer.append(" and a.jobTitleCode=?");
			parameter.putParameterObject(jobTitleInfo.getJobTitleCode());
		}
		if (StringUtils.isNotNullAndBlank(jobTitleInfo.getJobTitleName()))
		{
			sqlBuffer.append(" and a.jobTitleName=?");
			parameter.putParameterObject(jobTitleInfo.getJobTitleName());
		}
		if (StringUtils.isNotNullAndBlank(jobTitleInfo.getJobTitleDesc()))
		{
			sqlBuffer.append(" and a.jobTitleDesc=?");
			parameter.putParameterObject(jobTitleInfo.getJobTitleDesc());
		}
		if (StringUtils.isNotNullAndBlank(jobTitleInfo.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(jobTitleInfo.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(jobTitleInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(jobTitleInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询职称数据,出参:List<JobTitlePO>=[{}]====", returnList);
		logger.info("====查询职称数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(JobTitlePO jobTitlePO)
	{
		logger.info("====删除职称数据开始====");
		logger.debug("====删除职称数据,入参:JobTitlePO=[{}]====", jobTitlePO);
		entityManager.remove(jobTitlePO);
		logger.debug("====删除职称数据,出参:boolean=[{}]====", true);
		logger.info("====删除职称数据结束====");
		return true;
	}

	@Override
	public Long count(JobTitlePO jobTitlePO)
	{
		logger.info("====统计职称数据开始====");
		logger.debug("====统计职称数据,入参:JobTitlePO=[{}]====", jobTitlePO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from JobTitlePO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(jobTitlePO.getJobTitleID()))
		{
			sqlBuffer.append(" and a.jobTitleID=?");
			parameter.putParameterObject(jobTitlePO.getJobTitleID());
		}
		if (StringUtils.isNotNullAndBlank(jobTitlePO.getJobTitleCode()))
		{
			sqlBuffer.append(" and a.jobTitleCode=?");
			parameter.putParameterObject(jobTitlePO.getJobTitleCode());
		}
		if (StringUtils.isNotNullAndBlank(jobTitlePO.getJobTitleName()))
		{
			sqlBuffer.append(" and a.jobTitleName=?");
			parameter.putParameterObject(jobTitlePO.getJobTitleName());
		}
		if (StringUtils.isNotNullAndBlank(jobTitlePO.getJobTitleDesc()))
		{
			sqlBuffer.append(" and a.jobTitleDesc=?");
			parameter.putParameterObject(jobTitlePO.getJobTitleDesc());
		}
		if (StringUtils.isNotNullAndBlank(jobTitlePO.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(jobTitlePO.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(jobTitlePO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(jobTitlePO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计职称数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计职称数据结束====");
		return returnLong;
	}

}
